home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / CPP / WFC010.ZIP / SRC / CNETFILE.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-07  |  5.5 KB  |  254 lines

  1. #include <wfc.h>
  2. #pragma hdrstop
  3.  
  4. /*
  5. ** Author: Samuel R. Blackburn
  6. ** CI$: 76300,326
  7. ** Internet: sammy@sed.csc.com
  8. **
  9. ** You can use it any way you like as long as you don't try to sell it.
  10. **
  11. ** Any attempt to sell WFC in source code form must have the permission
  12. ** of the original author. You can produce commercial executables with
  13. ** WFC but you can't sell WFC.
  14. **
  15. ** Copyright, 1995, Samuel R. Blackburn
  16. **
  17. ** $Workfile: $
  18. ** $Revision: $
  19. ** $Modtime: $
  20. */
  21.  
  22. #if defined( _DEBUG )
  23. #undef THIS_FILE
  24. static char BASED_CODE THIS_FILE[] = __FILE__;
  25. #endif
  26.  
  27. IMPLEMENT_SERIAL( CNetworkFileInformation, CObject, 1 )
  28. IMPLEMENT_SERIAL( CNetworkFiles, CNetwork, 1 )
  29.  
  30. #if defined( _DEBUG )
  31. #define new DEBUG_NEW
  32. #endif
  33.  
  34. /*
  35. ** CNetworkFileInformation stuff
  36. */
  37.  
  38. CNetworkFileInformation::CNetworkFileInformation()
  39. {
  40.    m_Initialize();
  41. }
  42.  
  43. CNetworkFileInformation::CNetworkFileInformation( FILE_INFO_3 *source )
  44. {
  45.    Copy( source );
  46. }
  47.  
  48. CNetworkFileInformation::CNetworkFileInformation( const CNetworkFileInformation& source )
  49. {
  50.    Copy( source );
  51. }
  52.  
  53. CNetworkFileInformation::~CNetworkFileInformation()
  54. {
  55.    m_Initialize();
  56. }
  57.  
  58. void CNetworkFileInformation::Copy( FILE_INFO_3 *source )
  59. {
  60.    ASSERT( source != NULL );
  61.  
  62.    if ( source == NULL )
  63.    {
  64.       m_Initialize();
  65.       return;
  66.    }
  67.  
  68. #if ! defined( UNICODE )
  69.    ::UNICODE_to_ASCII( (LPCWSTR) source->fi3_pathname, source->fi3_pathname );
  70.    ::UNICODE_to_ASCII( (LPCWSTR) source->fi3_username, source->fi3_username );
  71. #endif
  72.  
  73.    ID            = source->fi3_id;
  74.    Permissions   = source->fi3_permissions;
  75.    NumberOfLocks = source->fi3_num_locks;
  76.    PathName      = source->fi3_pathname;
  77.    UserName      = source->fi3_username;
  78.  
  79. #if ! defined( UNICODE )
  80.    ::ASCII_to_UNICODE( source->fi3_pathname, (LPWSTR) source->fi3_pathname );
  81.    ::ASCII_to_UNICODE( source->fi3_username, (LPWSTR) source->fi3_username );
  82. #endif
  83. }
  84.  
  85. void CNetworkFileInformation::Copy( const CNetworkFileInformation& source )
  86. {
  87.    ASSERT( this != &source );
  88.  
  89.    /*
  90.    ** Make sure we ain't copying ourselves
  91.    */
  92.  
  93.    if ( this == &source )
  94.    {
  95.       return;
  96.    }
  97.  
  98.    ID            = source.ID;
  99.    Permissions   = source.Permissions;
  100.    NumberOfLocks = source.NumberOfLocks;
  101.    PathName      = source.PathName;
  102.    UserName      = source.UserName;
  103. }
  104.  
  105. void CNetworkFileInformation::Empty( void )
  106. {
  107.    m_Initialize();
  108. }
  109.  
  110. void CNetworkFileInformation::m_Initialize( void )
  111. {
  112.    UserName.Empty();
  113.    PathName.Empty();
  114.    ID            = 0;
  115.    Permissions   = 0;
  116.    NumberOfLocks = 0;
  117. }
  118.  
  119. void CNetworkFileInformation::Serialize( CArchive& archive )
  120. {
  121.    CObject::Serialize( archive );
  122.  
  123.    if ( archive.IsStoring() )
  124.    {
  125.       archive << UserName;
  126.       archive << PathName;
  127.       archive << ID;
  128.       archive << Permissions;
  129.       archive << NumberOfLocks;
  130.    }
  131.    else
  132.    {
  133.       archive >> UserName;
  134.       archive >> PathName;
  135.       archive >> ID;
  136.       archive >> Permissions;
  137.       archive >> NumberOfLocks;
  138.    }
  139. }
  140.  
  141. /*
  142. ** CNetworkFiles Stuff
  143. */
  144.  
  145. CNetworkFiles::CNetworkFiles()
  146. {
  147.    m_Initialize();
  148. }
  149.  
  150. CNetworkFiles::CNetworkFiles( LPCTSTR machine_name )
  151. {
  152.    m_Initialize();
  153.    Open( machine_name );
  154. }
  155.  
  156. CNetworkFiles::~CNetworkFiles()
  157. {
  158.    Close();
  159.    m_Initialize();
  160. }
  161.  
  162. void CNetworkFiles::Close( void )
  163. {
  164.    CNetwork::Close();
  165.  
  166.    if ( m_103InformationBuffer != NULL )
  167.    {
  168.       ::NetApiBufferFree( m_103InformationBuffer );
  169.       m_103InformationBuffer = NULL;
  170.    }
  171. }
  172.  
  173. BOOL CNetworkFiles::Close( CNetworkFileInformation& information )
  174. {
  175.    m_ErrorCode = ::NetFileClose( (LPTSTR) m_WideMachineName, information.ID );
  176.  
  177.    if ( m_ErrorCode == NERR_Success )
  178.    {
  179.       return( TRUE );
  180.    }
  181.    else
  182.    {
  183.       return( FALSE );
  184.    }
  185. }
  186.  
  187. void CNetworkFiles::m_Initialize( void )
  188. {
  189.    m_ErrorCode               = 0;
  190.    m_103InformationBuffer    = NULL;
  191.    m_103ResumeHandle         = 0;
  192.    m_103CurrentEntryNumber   = 0;
  193.    m_103NumberOfEntriesRead  = 0;
  194.    m_103TotalNumberOfEntries = 0;
  195. }
  196.  
  197. BOOL CNetworkFiles::Enumerate( void )
  198. {
  199.    if ( m_103InformationBuffer != NULL )
  200.    {
  201.       ::NetApiBufferFree( m_103InformationBuffer );
  202.       m_103InformationBuffer = NULL;
  203.    }
  204.  
  205.    m_103CurrentEntryNumber   = 0;
  206.    m_103NumberOfEntriesRead  = 0;
  207.    m_103ResumeHandle         = 0;
  208.    m_103TotalNumberOfEntries = 0;
  209.  
  210.    m_ErrorCode = ::NetFileEnum( (LPTSTR) m_WideMachineName,
  211.                                          NULL, 
  212.                                          NULL, 
  213.                                          3, 
  214.                              (LPBYTE *) &m_103InformationBuffer,
  215.                                          65535,
  216.                                         &m_103NumberOfEntriesRead,
  217.                                         &m_103TotalNumberOfEntries,
  218.                                         &m_103ResumeHandle );
  219.  
  220.    if ( m_ErrorCode != NERR_Success || m_103InformationBuffer == NULL )
  221.    {
  222.       return( FALSE );
  223.    }
  224.  
  225.    return( TRUE );
  226. }
  227.  
  228. BOOL CNetworkFiles::GetNext( CNetworkFileInformation& information )
  229. {
  230.    if ( m_103CurrentEntryNumber < m_103TotalNumberOfEntries )
  231.    {
  232.       information.Copy( &m_103InformationBuffer[ m_103CurrentEntryNumber ] );
  233.       m_103CurrentEntryNumber++;
  234.       return( TRUE );
  235.    }
  236.  
  237.    information.Empty();
  238.    return( FALSE );
  239. }
  240.  
  241. void CNetworkFiles::Serialize( CArchive& archive )
  242. {
  243.    CNetwork::Serialize( archive );
  244.  
  245.    if ( archive.IsStoring() )
  246.    {
  247.       archive << m_ErrorCode;
  248.    }
  249.    else
  250.    {
  251.       archive >> m_ErrorCode;
  252.    }
  253. }
  254.